import { legacy_createStore as createStore, combineReducers, applyMiddleware } from 'redux'
// 引入模块化购物车reducer
import carReducer from './car/carReducer'
// 引入模块化用户reducer
import userReducer from './user/userReducer'
// 引入插件
import logger from 'redux-logger'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage' // defaults to localStorage for web

// 配置
const persistConfig = {
  key: 'root',
  storage,
}

// 合并reducer
const reducer = combineReducers({
	car: carReducer,
	user: userReducer
})

const persistedReducer = persistReducer(persistConfig, reducer)

// 创建仓库
const store = createStore(persistedReducer, applyMiddleware(logger))
export const persistor = persistStore(store)
// 仓库类型
export type RootState = ReturnType<typeof store.getState>
 
export default store